package com.dzh.healthflutter.flutter;

/**
 * @Description: 描述
 * @Author: dzh
 * @CreateDate: 2021-07-24 22:22
 */

import android.app.Activity;
import android.content.Intent;

import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle.Event;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import org.jetbrains.annotations.NotNull;

import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.platform.PlatformPlugin;


public final class FlutterViewEngine implements LifecycleObserver {
    private FlutterView flutterView;
    private ComponentActivity activity;
    private PlatformPlugin platformPlugin;
    @NotNull
    private final FlutterEngine engine;

    private final void hookActivityAndView() {
        ComponentActivity var10000 = this.activity;
        if (var10000 == null) {
            return;
        }

        if (flutterView == null) {
            return;
        }

        this.platformPlugin = new PlatformPlugin((Activity) var10000, this.engine.getPlatformChannel());
        this.engine.getActivityControlSurface().attachToActivity((Activity) var10000, var10000.getLifecycle());
        flutterView.attachToFlutterEngine(this.engine);
        var10000.getLifecycle().addObserver((LifecycleObserver) this);
    }

    private final void unhookActivityAndView() {
        ComponentActivity var10000 = this.activity;
        if (var10000 == null) {
            return;
        }

        var10000.getLifecycle().removeObserver((LifecycleObserver) this);
        this.engine.getActivityControlSurface().detachFromActivity();
        PlatformPlugin var1 = this.platformPlugin;
        if (var1 == null) {
            return;
        }

        var1.destroy();
        this.platformPlugin = (PlatformPlugin) null;
        this.engine.getLifecycleChannel().appIsDetached();
        FlutterView var2 = this.flutterView;
        if (var2 == null) {
            return;
        }

        var2.detachFromFlutterEngine();
    }

    public final void attachToActivity(@NotNull ComponentActivity activity) {
//        Intrinsics.checkParameterIsNotNull(activity, "activity");
        this.activity = activity;
        if (this.flutterView != null) {
            this.hookActivityAndView();
        }

    }

    public final void detachActivity() {
        if (this.flutterView != null) {
            this.unhookActivityAndView();
        }

        this.activity = (ComponentActivity) null;
    }

    public final void attachFlutterView(@NotNull FlutterView flutterView) {
//        Intrinsics.checkParameterIsNotNull(flutterView, "flutterView");
        this.flutterView = flutterView;
        if (this.activity != null) {
            this.hookActivityAndView();
        }

    }

    public final void detachFlutterView() {
        this.unhookActivityAndView();
        this.flutterView = (FlutterView) null;
    }

    @OnLifecycleEvent(Event.ON_RESUME)
    private final void resumeActivity() {
        if (this.activity != null) {
            this.engine.getLifecycleChannel().appIsResumed();
        }

        PlatformPlugin var10000 = this.platformPlugin;
        if (var10000 != null) {
            var10000.updateSystemUiOverlays();
        }

    }

    @OnLifecycleEvent(Event.ON_PAUSE)
    private final void pauseActivity() {
        if (this.activity != null) {
            this.engine.getLifecycleChannel().appIsInactive();
        }

    }

    @OnLifecycleEvent(Event.ON_STOP)
    private final void stopActivity() {
        if (this.activity != null) {
            this.engine.getLifecycleChannel().appIsPaused();
        }

    }

    public final void onRequestPermissionsResult(int requestCode, @NotNull String[] permissions, @NotNull int[] grantResults) {
//        Intrinsics.checkParameterIsNotNull(permissions, "permissions");
//        Intrinsics.checkParameterIsNotNull(grantResults, "grantResults");
        if (this.activity != null && this.flutterView != null) {
            this.engine.getActivityControlSurface().onRequestPermissionsResult(requestCode, permissions, grantResults);
        }

    }

    public final void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (this.activity != null && this.flutterView != null) {
            this.engine.getActivityControlSurface().onActivityResult(requestCode, resultCode, data);
        }

    }

    public final void onUserLeaveHint() {
        if (this.activity != null && this.flutterView != null) {
            this.engine.getActivityControlSurface().onUserLeaveHint();
        }

    }

    @NotNull
    public final FlutterEngine getEngine() {
        return this.engine;
    }

    public FlutterViewEngine(@NotNull FlutterEngine engine) {
//        Intrinsics.checkParameterIsNotNull(engine, "engine");
        super();
        this.engine = engine;
    }
}
